package org.lmind.example.common.web;

import org.lmind.example.util.common.ThreadBinderProxyFactory;

public class DefaultSessionContextManager implements SessionContextManager {
	
	private ThreadBinderProxyFactory<SessionContext> threadBinderProxyFactory = new ThreadBinderProxyFactory<SessionContext>();
	
	private volatile SessionContext proxy;
	
	public DefaultSessionContextManager() {
		threadBinderProxyFactory.setInterfaceClass(SessionContext.class);
	}

	public SessionContext getCurrentThread() {
		if (proxy == null) {
			synchronized (this) {
				if (proxy == null) {
					proxy = threadBinderProxyFactory.newProxy();
				}
			}
		}
		return proxy;
	}

	public void bindCurrentThread(SessionContext sessionContext) {
		threadBinderProxyFactory.bind(sessionContext);
	}
	
	public void cleanCurrentThread() {
		threadBinderProxyFactory.unbind();
	}
}
